# coding:utf-8

import socket

from multiprocessing import Process

import multiprocessing as mul


def proc1(pipe):
    while True:
        pipe.send('hello')
        print('proc1 rec:', pipe.recv())




def handle_client(client_socket,pipe):
    """
    处理客户端请求
    """
    while True:
        request_data = client_socket.recv(1024)
        print("request data:", request_data)
        client_socket.send(bytes("response", "utf-8"))
        pipe.send('hello')
        print('proc1 rec:', pipe.recv())

if __name__ == "__main__":
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind(("", 8000))
    server_socket.listen(8)
    pipe = mul.Pipe()
    # Pass an end of the pipe to process 1


    while True:
        client_socket, client_address = server_socket.accept()
        print(client_socket)
        print("[%s, %s]用户连接上了" % client_address)
        p2 = Process(target=handle_client, args=(client_socket,pipe[1]))
        p2.start()
        p1 = mul.Process(target=proc1, args=(pipe[0],))
        # Pass the other end of the pipe to process 2
        #p2 = mul.Process(target=proc2, args=(pipe[1],))
        p1.start()
        #p2.start()
        p1.join()
        p2.join()
        # p2.join()
        #client_socket.close()